package com.operator.Activitys.view.pay.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.operator.Activitys.R;
import com.operator.Activitys.view.pay.listener.OnPasswordInputFinish;

/**
 * 弹框里面的View
 */
public class PasswordView extends RelativeLayout {

    //全局上下文
    private  Context mContext;
    //用数组保存6个TextView
    private TextView[] tvList;
    //用数组保存6个TextView
    private ImageView[] imgList;


    public PasswordView(Context context) {
        this(context, null);
    }

    public PasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        //获取布局控件
        View view = View.inflate(context, R.layout.layout_pwd, null);

        //获取布局中的控件
        initView(view);
        //添加View
        addView(view);
    }

    private void initView(View view) {

        //实例化 文字控件集合
        tvList = new TextView[6];
        //实例化 图片控件集合
        imgList = new ImageView[6];

        //获取 文字 视图
        tvList[0] = (TextView) view.findViewById(R.id.tv_pass1);
        tvList[1] = (TextView) view.findViewById(R.id.tv_pass2);
        tvList[2] = (TextView) view.findViewById(R.id.tv_pass3);
        tvList[3] = (TextView) view.findViewById(R.id.tv_pass4);
        tvList[4] = (TextView) view.findViewById(R.id.tv_pass5);
        tvList[5] = (TextView) view.findViewById(R.id.tv_pass6);

        //获取 图片 视图
        imgList[0] = (ImageView) view.findViewById(R.id.img_pass1);
        imgList[1] = (ImageView) view.findViewById(R.id.img_pass2);
        imgList[2] = (ImageView) view.findViewById(R.id.img_pass3);
        imgList[3] = (ImageView) view.findViewById(R.id.img_pass4);
        imgList[4] = (ImageView) view.findViewById(R.id.img_pass5);
        imgList[5] = (ImageView) view.findViewById(R.id.img_pass6);

    }

    /**
     * 联动软件盘 输入
     * @param strFlag
     * @param position
     * @param currentIndex
     */
    public void setupView(String strFlag,int position,int currentIndex,String name) {

        switch (strFlag){

            case "add":
                tvList[currentIndex].setText(name);

                tvList[currentIndex].setVisibility(View.INVISIBLE);
                imgList[currentIndex].setVisibility(View.VISIBLE);
                break;

            case "delete":
                tvList[currentIndex].setText("");

                tvList[currentIndex].setVisibility(View.VISIBLE);
                imgList[currentIndex].setVisibility(View.INVISIBLE);
                break;
        }
    }

    //设置监听方法，在第6位输入完成后触发
    public void setOnFinishInput(final OnPasswordInputFinish pass) {

        tvList[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().length() == 1) {

                    //每次触发都要先将strPassword置空，再重新获取，避免由于输入删除再输入造成混乱
                    String strPassword = "";

                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }
                    //接口中要实现的方法，完成密码输入完成后的响应逻辑
                    pass.inputFinish(strPassword);
                }
            }
        });
    }

    /**
     * 清空数据源
     */
    public void setClearData(){
        for (int i = 0;i<tvList.length;i++){
            tvList[i].setText("");
            tvList[i].setVisibility(View.VISIBLE);
            imgList[i].setVisibility(View.INVISIBLE);
        }
    }
}
